package com.googlecode.compliancechecker;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * Wrapper class for an android jar file that provides utility methods.
 * 
 * @author Marc-Christian Schulze
 * 
 */
public class AndroidJar {
	private JarFile jarFile;

	public AndroidJar(File androidHome, int apiLevel) throws IOException {
		File androidJarFile = getAndroidJarFile(androidHome, apiLevel);
		if (!androidJarFile.exists()) {
			throw new FileNotFoundException("Couldn't find " + androidJarFile);
		}
		jarFile = new JarFile(androidJarFile);
	}

	private static File getAndroidJarFile(File androidHome, int apiLevel) {
		String relativePath = "platforms/android-" + apiLevel + "/android.jar";
		return new File(androidHome, relativePath);
	}

	public InputStream openClass(String className) throws IOException {
		JarEntry entry = jarFile.getJarEntry(className + ".class");
		if (entry == null) {
			return null;
		}
		return jarFile.getInputStream(entry);
	}
}
